﻿using System;
using System.Web.UI;
using Microsoft.Office.Server.Search.Query;
using Trentacular.SharePoint.Reflection;
using System.Reflection;

namespace Trentacular.Office.Server.Search.FacetedSearch.QueryRetrieval
{
    /// <summary>
    /// 
    /// </summary>
    public class SP2010QueryRetriever : ReflectingObjectWrapper, IQueryRetriever
    {
        private Page page;

        /// <summary>
        /// Initializes a new instance of the <see cref="SP2010QueryRetriever"/> class.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        public SP2010QueryRetriever(Page currentPage)
        {
            this.page = currentPage;
        }

        private object _queryManager;

        /// <summary>
        /// Gets the QueryManager object.
        /// </summary>
        /// <value>The target QueryManager object.</value>
        protected override object Target
        {
            get
            {
                if (_queryManager == null)
                {
                    //SharedQueryManager.GetInstance(this.page).QueryManager;
                    var sharedQueryManager = typeof(Query).Assembly.GetType(
                        "Microsoft.Office.Server.Search.WebControls.SharedQueryManager"
                        )
                        .GetMethod(
                            "GetInstance",
                            BindingFlags.Public | BindingFlags.Static
                            )
                        .Invoke(null, new object[] { this.page });

                    _queryManager = sharedQueryManager.GetType()
                        .GetProperty("QueryManager")
                        .GetValue(sharedQueryManager, null);

                    if (_queryManager == null)
                        throw new InvalidOperationException(
                            "Failed to retrieve the query from the page."
                            );
                }
                return _queryManager;
            }
        }

        #region IQueryRetriever Implementation 
        
        /// <summary>
        /// Retrieves the query that will mimic the query used to execute the main
        /// search but with the select clause replaced the given managed properties.
        /// </summary>
        /// <param name="managedPropertiesToQuery"></param>
        /// <returns>
        /// A query that will mimic the query used to execute the main search
        /// but with the select clause replaced the given managed properties.
        /// </returns>
        public Query RetrieveQuery(string[] managedPropertiesToQuery)
        {
            

            // TODO: Posisbly support 2010 if possible?
            throw new NotSupportedException("SP 2010 is not yet supported");
        }

        /// <summary>
        /// Gets the query text.
        /// </summary>
        /// <value>The query text.</value>
        public string QueryText
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
